<template>
  <el-dialog
    class="dialogStyle"
    style="width: 70vw"
    :title="'全局报警'"
    v-model="visible"
  >
    <el-button @click="okBtn">一键确认</el-button>

    <!-- 表格 -->
    <el-table
      :data="tableData"
      stripe
      height="69vh"
      border
      style="width: 100%"
      :header-cell-style="{ background: '#fafafa', color: '#333' }"
    >
      <el-table-column align="center" type="index" fixed="left" />
      <el-table-column align="center" prop="xxx" label="区队" min-width="100" />
      <el-table-column align="center" prop="xxx" label="设备" min-width="100" />
      <el-table-column align="center" prop="xxx" label="页面" min-width="100" />
      <el-table-column align="center" prop="xxx" label="描述" min-width="100" />
      <el-table-column align="center" prop="xxx" label="时间" min-width="100" />
      <el-table-column
        align="center"
        prop="xxx"
        label="报警值"
        min-width="100"
      />
      <el-table-column
        align="center"
        prop="xxx"
        label="持续时间"
        min-width="100"
      />
      <el-table-column
        align="center"
        prop="xxx"
        label="恢复值"
        min-width="100"
      />
      <el-table-column
        align="center"
        prop="xxx"
        label="恢复时间"
        min-width="100"
      />
      <el-table-column
        align="center"
        prop="xxx"
        label="恢复状态"
        min-width="100"
      />
      <el-table-column
        align="center"
        prop="userStatus"
        label="状态"
        min-width="100"
      >
        <template #default="scope">
          <div v-if="scope.row.userStatus == '1'">
            <span class="istyleOk"></span>启用
          </div>
          <div v-if="scope.row.userStatus == '0'">
            <span class="istyleNo"></span>禁用
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 底部的确认、取消按钮 -->
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancleBtn">{{ "取消" }}</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">
          {{ "确定" }}
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
// import {
//   apiAddUser,
//   apiPutUserInfo,
//   apiGetUser,
//   apiAllRoleUser,
// } from "@/apis/user";
// import emitter from "@/utils/eventbus"; //引入bus

// 定义变量内容
const visible = ref(false);
const loading = ref(false);
const dataFormRef = ref(); // form表单的ref
const roleArr = ref(); //角色数组

// 提交表单数据
const form = reactive({
  userId: "",
  roleId: 1,
  userNo: "",
  userPwd: "123",
  realName: "",
  telephone: "",
  userStatus: "1",
  userSex: "1",
  userAge: "",
});
// 清空表单
const clearForm = () => {
  form.userId = "";
  form.roleId = 1;
  form.userNo = "";
  form.userPwd = "123";
  form.realName = "";
  form.telephone = "";
  form.userStatus = "1";
  form.userSex = "1";
  form.userAge = "";
};
// 校验规则
const rules = {
  userNo: [
    {
      required: true,
      message: "工号不可为空",
    },
  ],
  realName: [
    {
      required: true,
      message: "姓名不可为空",
    },
    {
      min: 2,
      max: 5,
      message: "姓名长度必须在2到5之间",
    },
  ],
  telephone: [
    {
      required: true,
      message: "电话不可为空",
    },
    {
      pattern: /^[1][3,4,5,7,8,9][0-9]{9}$/,
      message: "电话格式不正确",
    },
  ],
  userPwd: [
    {
      required: true,
      message: "密码不可为空",
    },
  ],
  userSex: [
    {
      required: true,
      message: "性别不可为空",
    },
  ],
  userAge: [
    {
      required: true,
      message: "年龄不可为空",
    },
  ],
  roleId: [
    {
      required: true,
      message: "角色不可为空",
    },
  ],
  userStatus: [
    {
      required: true,
      message: "状态不可为空",
    },
  ],
};

// 打开弹窗
const openDialog = (tempId: string) => {
  visible.value = true; // 打开弹窗
  form.userId = ""; // 防止上次的操作还在表单上，影响本次操作
  // 重置表单数据---否则新增弹窗会自动触发校验
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取信息
  if (tempId) {
    // 如果有id，就编辑逻辑
    form.userId = tempId;
    getElevatorInfoData(tempId); // 初始化表单数据的方法
  } else {
    // 添加逻辑
    clearForm(); //清空表单
  }
};
// 编辑：初始化表单数据
const getElevatorInfoData = (tempId: string) => {
  loading.value = true;
  apiGetUser(tempId) // 编辑回显接口
    .then((res: any) => {
      Object.assign(form, res.data); // 将接口数据赋值到本地
    })
    .finally(() => {
      loading.value = false;
    });
};

// 提交表单
const onSubmit = async () => {
  // 表单校验
  // const valid = await dataFormRef.value.validate().catch(() => {});
  // if (!valid) return false;

  try {
    loading.value = true;
    // 如果有id，就修改接口，否则新增接口
    // form.userId
    //   ? await apiPutUserInfo(form)
    //       .then((res: any) => {
    //         if (res.code == 100) {
    //           // 提示框
    //           ElMessage({
    //             message: "修改成功",
    //             type: "success",
    //           });
    //         } else {
    //           ElMessage({
    //             message: res.msg,
    //             type: "error",
    //           });
    //         }
    //       })
    //       .catch((err) => {
    //         ElMessage.error(err.message);
    //       })
    //   : await apiAddUser(form)
    //       .then((res: any) => {
    //         if (res.code == 100) {
    //           // 提示框
    //           ElMessage({
    //             message: "新增成功",
    //             type: "success",
    //           });
    //         } else {
    //           ElMessage({
    //             message: res.msg,
    //             type: "error",
    //           });
    //         }
    //       })
    //       .catch((err) => {
    //         ElMessage.error(err.message);
    //       });
    // visible.value = false;
    // emitter.emit("refresh"); //调用父组件刷新方法
  } catch (err: any) {
    ElMessage.error(err.msg);
  } finally {
    loading.value = false;
  }
};

// 取消按钮
const cancleBtn = () => {
  visible.value = false;
};

const okBtn = () => {
  ElMessage.info("okBtn");
};
// 暴露变量
defineExpose({
  openDialog,
});
</script>

<style scoped lang="scss">
.dialogStyle {
}

.formstyle {
  padding: 0 80px;
}
</style>
